<?php
namespace App\Http\Controllers\ApiApp\Stock;

use App\DBs\Stock\PlusDB;
use Illuminate\Http\Request;

class PlusController extends BaseController
{
    /**
     * 库
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new PlusDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        if (!$uid) {
            return $this->toAppJson(-2);
        }
        $resDb = $this->db->getAppIndex($uid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5,array(),$resDb['msg']);
        }
        $dataArr = $resDb['data'];
        return $this->toAppJson(0, $dataArr);
    }

    /**
     * 创作增加
     */
    public function setAdd(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        $vid = $request->post('vid',0);
        if (!$uid || !$vid) {
            return $this->toAppJson(-2);
        }
        //看看有没有，有的话，直接返回
        $resDb3 = $this->db->getAppOneByUid($uid,$vid);
        if ($resDb3['code']==0) {
            $dataArr = $resDb3['data'];
            return $this->toAppJson(0, $dataArr, '数据已存在');
        }
        //没有则重新添加
        $resDb = $this->db->setAdd($uid,$vid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-6,array(),$resDb['msg']);
        }
        //返回的信息
        $plusId = $resDb['data']['lastId'];
        $resDb2 = $this->db->getAppOne($plusId,$uid);
        if ($resDb2['code']!=0) {
            return $this->toAppJson(-6,array(),$resDb['msg']);
        }
        $dataArr = $resDb2['data'];
        return $this->toAppJson(0, $dataArr, '新增成功');
    }
}
